Introduction This TechNote discusses how to format date
values while developing in the PHP scripting
language. Formatting date values involves hand
coding as described in the instructions below.
Please note that this TechNote is for information
purposes only. Hand coding is considered an
advanced use of Dreamweaver and is not supported
by Macromedia Technical Support.
PHP functions used to format date
and time values The following
describes the functions developers can use for
displaying and working with date in their PHP
coded pages.
.iso/tutorial/macromed/files/dreamwv/datetime_files/tri.gif) |
date(format, timestamp
value) the date() function formats a
valid timestamp value according to the specified
date and/or time format. The date() function
accepts two parameters, the date/time format and
a valid timestamp value.
Below is a table of the most common
format options used with the date() function.
Other format options, as well as references to
other PHP functions can be found at PHP.NET.
Format Character |
Description |
Returned value |
d |
Day of the month, 2 digits with leading
zeros |
01 to 31 |
D |
A textual representation of a week, three
letters |
Mon through Sun |
F |
A full textual representation of a month,
such as January or March |
January through December |
j |
Day of the month without leading
zeros |
1 to 31 |
l (lower case L) |
A full textual representation of the day
of the week |
Sunday through Saturday |
M |
A short textual representation of a
month, three letters |
Jan through Dec |
S |
English ordinal suffix for the day of the
month, 2 characters |
st, nd, rd or th (Works well with
j) |
y |
A two digit representation of a
year |
Examples: 99 or 03 |
Y |
A full numeric representation of a year,
4 digits |
Examples: 1999 or
2003 | |
.iso/tutorial/macromed/files/dreamwv/datetime_files/tri.gif) |
strtotime(date/time
value) the strtotime() function
parses a string date/time description into a
valid UNIX timestamp. |
|
The following is an example of the usage
of both functions: <?
$dateValue = "March 28, 1973";
$formattedDate = date("l, jS F Y",
strtotime($dateValue));
?>
<?php echo $dateValue; ?> <br>
<?php echo $formattedDate; ?> |
To format date and time values in PHP, do
the following steps:
1 |
Select View > Code and
Design. |
2 |
Select the date or time value on the
page. You should also see its corresponding
source code selected in code view.
The following code displays a column
called startdate from a recordset called
rsDetails in which the data is stored as a
string (rather than as an integer): <?php echo $row_rsDetails['startdate'];?> |
3 |
In order to manipulate the display of
this data as a date, you must first convert it
to a date. This is done with the strtotime()
function.
Add the strtotime() function to the code
by identifying the code which must be converted
to a date, then insert that code as a value
inside the strtotime() function. Your code
should be similar to the code below (the code in
red has been added from the previous
step). <?php echo strtotime($row_rsDetails['startdate']);?> |
|
Note: The step above should only be completed
if the data you are starting with is in string
format. If the data is already stored as an
integer or is in a string format that doesn't
convert to a date, using the strtotime()
function will return a failure value of -1. This
will cause the next step to
fail. |
4 |
Now that the data has been converted from
string format to a date, format the date itself
with the date() function (the code in red has
been added from the previous step). Use the
formatting choices describe at the top of this
TechNote for your date formatting. <?php echo date("l, jS F Y",strtotime
($row_rsDetails ['startdate']));?> |
5 |
Save your page and preview it in a
browser. You should now see the formatted code.
The code described in these steps produces the
following format: Thursday, 22nd May 2003 |
|